//
//  TableViewCell.m
//  轻量级TableView
//
//  Created by 徐益智 on 16/4/14.
//  Copyright © 2016年 Vdianjing. All rights reserved.
//

#import "TableViewCell.h"

#import "UIView+SDAutoLayout.h"

@interface TableViewCell ()

@property (nonatomic, strong)UILabel *nameLabel;
@property (nonatomic, strong)UILabel *contentLabel;
@property (nonatomic, strong)UIImageView *headerImageView;
@property (nonatomic, strong)UIImageView *picture;

@end

@implementation TableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initBody];
    }
    return self;
}

- (void)initBody
{
    UIView *contentView = self.contentView;
    [contentView addSubview:self.headerImageView];
    [contentView addSubview:self.nameLabel];
    [contentView addSubview:self.contentLabel];
    [contentView addSubview:self.picture];
    
    CGFloat orig = 10.f;
    self.headerImageView.sd_layout
    .leftEqualToView(contentView)
    .topSpaceToView(contentView, orig)
    .heightIs(30)
    .widthEqualToHeight();
    
    self.nameLabel.sd_layout
    .leftSpaceToView(self.headerImageView, orig)
    .rightEqualToView(contentView)
    .topSpaceToView(contentView, orig)
    .heightIs(30);
    
    self.contentLabel.sd_layout
    .leftEqualToView(self.nameLabel)
    .topSpaceToView(self.nameLabel, orig)
    .rightEqualToView(contentView)
    .autoHeightRatio(0);
    
    self.picture.sd_layout
    .leftEqualToView(self.nameLabel)
    .rightSpaceToView(contentView, orig)
    .topSpaceToView (self.contentLabel, orig)
    .heightEqualToWidth();
}

- (void)setModel:(Model *)model
{
    self.headerImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", model.icon]];
    self.nameLabel.text = model.name;
    self.contentLabel.text = model.text;
    
    
    if (model.picture) {
        [self.picture setHidden:NO];
        self.picture.image = [UIImage imageNamed:model.picture];
        [self setupAutoHeightWithBottomView:self.picture bottomMargin:10];
    }else {
        [self.picture setHidden:YES];
        [self setupAutoHeightWithBottomView:self.contentLabel bottomMargin:10];
    }
}


#pragma mark - getter
- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = [UIColor blackColor];
        _nameLabel.backgroundColor = [UIColor clearColor];
    }
    return _nameLabel;
}

- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.textColor = [UIColor blackColor];
        _contentLabel.backgroundColor = [UIColor clearColor];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}

- (UIImageView *)headerImageView
{
    if (!_headerImageView) {
        _headerImageView = [[UIImageView alloc] init];
    }
    return _headerImageView;
}

- (UIImageView *)picture
{
    if (!_picture) {
        _picture = [[UIImageView alloc] init];
    }
    return _picture;
}

@end
